import router from './index'

import store from '@/store'

import Vue from 'vue'
import VueRouter from 'vue-router'



//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)

//----判断路由权限
router.beforeEach((to, from, next) => {

    if (!store.state.UserToken) {
        //未登录 页面是否需要登录
        if (to.matched.length > 0 && !to.matched.some(record => record.meta.requiresAuth)) {
            next();
        } else {
            next({
                path: '/Login'
            })
        }
    } else {
        // 用户已经登录  路由的访问权限
        if (!store.state.premissions.permissionList) {
            store.dispatch('premissions/FETCH_PERMISSION').then(res => {
                next({
                    path: to.fullPath
                })
            })
        } else {
            // store 存在权限
            if (to.path !== '/Login') {
                next()
            } else {
                next(from.fullPath)
            }
        }
    }
}) 